package com.zhangkunhao.notes.controller;

import com.zhangkunhao.notes.bean.AdministratorInformation;
import com.zhangkunhao.notes.bean.ButtonName;
import com.zhangkunhao.notes.entity.DO.User;
import com.zhangkunhao.notes.service.UserService;

import com.zhangkunhao.notes.view.AdministratorsMenuView;

import com.zhangkunhao.notes.view.RegisterView;
import com.zhangkunhao.notes.view.UserMenuView;

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.List;

/**
 * 登录
 *
 * @author 张坤浩
 */
public class LoginListener implements ActionListener {
    /**
     * 账号输入框对象
     */
    private JTextField userName;
    /**
     * 密码输入框对象
     */
    private JPasswordField password;
    /**
     * 定义一个窗体对象
     */
    private JFrame loginFrame;

    public LoginListener(JFrame login, JTextField jt, JPasswordField jp) {
        //获取登录界面
        this.loginFrame = login;
        //获取登录界面中的账号输入框对象
        this.userName = jt;
        //获取登录界面中的密码输入框对象
        this.password = jp;
    }

    UserService userService = new UserService();
    /**
     * 将controller的得到的参数传到Service层
     */
    List<User> users = userService.queryList(0);

    @Override
    public void actionPerformed(ActionEvent e) {
        //将controller的得到的参数传到Service层
        users = userService.queryList(0);
        if (ButtonName.USER_LOGIN.equals(e.getActionCommand())) {
            //标志变量
            int loginFlag = 0;
            for (User user : users) {
                //用户名密码判断
                if (userName.getText().equals(user.getUserName()) && String.valueOf(password.getPassword()).equals(user.getPassword())) {
                    user.setUserName(user.getUserName());
                    user.setPassword(user.getPassword());

                    //设置弹框
                    JOptionPane.showMessageDialog(null, "登陆成功", "成功", 1);

                    //执行view层,传输登录成功的用户对象到UserMenuView
                    UserMenuView userMenuView = new UserMenuView(loginFrame, user);
                    userMenuView.initComponents();
                    loginFrame.setVisible(false);
                    loginFlag = 1;
                    break;
                }
            }
            if (loginFlag == 0) {
                JOptionPane.showMessageDialog(null, "登录名或密码错误", "失败", 0);
            }
        } else if (ButtonName.USER_REGISTER.equals(e.getActionCommand())) {
            //执行view 层
            RegisterView register = new RegisterView(loginFrame, users);

            try {
                loginFrame.setVisible(false);
                register.reShow();
            } catch (Exception ex) {
                ex.printStackTrace();
            }
        } else if (ButtonName.ADMINISTRATOR_LOGIN.equals(e.getActionCommand())) {
            if (AdministratorInformation.ADMINISTRATOR_ACCOUNT.equals(userName.getText())
                    && AdministratorInformation.ADMINISTRATOR_PASSWORD.equals(String.valueOf(password.getPassword()))) {
                //设置弹框
                JOptionPane.showMessageDialog(null, "登陆成功", "成功", 1);
                loginFrame.setVisible(false);
                AdministratorsMenuView administratorsMenuView = new AdministratorsMenuView(loginFrame);
                administratorsMenuView.initComponents();

            } else {
                JOptionPane.showMessageDialog(null, "操作失败", "系统提示", JOptionPane.WARNING_MESSAGE);
            }
        }
        System.out.println(e.getActionCommand());
    }
}


